package com.auxgroup.hr.front.commons.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

/**
 * @version V1.0
 * @desc AES 加密工具类
 */
public class AESUtil {
	
	private static final String PASSWORD = "YO4tvAx81hMcP1+xjj7+IHjMTWJBu2VM";
	
	
	/**
	 * 对加密内容进行解密
	 * @param content
	 * @return
	 */
	public static String decrypt(String content){
		try {
            SecretKeySpec key = new SecretKeySpec(PASSWORD.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            //与加密时不同MODE:Cipher.DECRYPT_MODE
            cipher.init(Cipher.DECRYPT_MODE, key);  //CBC类型的可以在第三个参数传递偏移量zeroIv,ECB没有偏移量
            byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(content));
            return new String(decryptedData, "utf-8");
        } catch (Exception e) {
            return null;
        }
	}
	
	
	
    public static void main(String[] args) {
    	String content = "0gmRjkuDgM%2FYfA2v6aScIzIBMzPXDstRcPLbtX8mGecOGYCoZu+Afr2YDyx0X5qghfXGUzEh1AAAeXOvcEBa6Q%3D%3D";
    	ScriptEngineManager sem = new ScriptEngineManager();
        ScriptEngine engine = sem.getEngineByExtension("js");
        try{
            //直接解析  
            Object res = engine.eval(" unescape('"+content+"')");  
            System.out.println(AESUtil.decrypt(res.toString()));
            System.out.println(res);  
        }catch(Exception ex){  
            ex.printStackTrace();  
        }  
    	
//    	System.out.println(content);
//    	String en = null;
//    	try {
//    		en = URLEncoder.encode(content, "UTF-8");
//			System.out.println(en);
//			en = URLDecoder.decode(en, "UTF-8");
//			System.out.println(en);
////			en = URLDecoder.decode(en, "UTF-8");
////			System.out.println(en);
////			System.out.println(AESUtil.decrypt(URLDecoder.decode(en, "UTF-8")));
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
    }

}